package office.service.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import current.security.user.domain.User;

import office.action.business.form.BidBookConditionForm;
import office.action.business.form.BidBookForm;
import office.action.business.wrapper.BidBookWrapper;
import office.action.workflow.form.WorkTaskForm;
import office.bo.business.impl.BidBook;
import office.domainset.BizDomainProviderSetImpl;
import office.service.business.IBidBookService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.DateUtil;
import platform.util.Util;

public class BidBookServiceImpl extends BaseBizServiceImpl implements IBidBookService{
	public void bidBookPriceApp(BidBookForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			BidBook bo = (BidBook)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getBidBookDP(), form.getId());
			bo =(BidBook) form.copyPropertiesToBOByEditFee(bo);
			//bo.setStatus(2);
			//保存待办任务(单独提供提交操作)
//			WorkTaskForm wtf=new WorkTaskForm();
//			wtf.setName(bo.getProjectName()+"标书费用申请");
//			wtf.setObjId(bo.getId().toString());
//			wtf.setObjClassName("BidBook");
//			wtf.setSubmitUser(bo.getApplyMan());
//			wtf.setSubmitDate(new Date());
//			wtf.setSubmitUserDeptId("loginUserDeptId");
//			//wtf.setHandleUser(handleUser);
//			wtf.setNodeState("财务审批");
//			wtf.setHandleState(0);
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑标书费用失败!", e);
		}
	}
	
	public void editBidBook(BidBookForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			BidBook bo = (BidBook)domainProviderSetImpl.getBidBookDP().newEmptyObj();
			bo =(BidBook) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
				//保存待办任务
				WorkTaskForm wtf=new WorkTaskForm();
				wtf.setName(bo.getProjectName()+"标书申请");
				wtf.setObjId(bo.getId().toString());
				wtf.setObjClassName("BidBook");
				wtf.setSubmitUser(bo.getApplyMan());
				wtf.setSubmitDate(new Date());
				wtf.setSubmitUserDeptId("loginUserDeptId");
				//wtf.setHandleUser(handleUser);
				wtf.setNodeState("财务审批");
				wtf.setHandleState(0);
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
			
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑标书失败!", e);
		}
	}
	
	public Page getPageInfo(BidBookConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getBidBookDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "BidBook.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProjectName())){
				condMap.put("projectName", condition.getProjectName());
			}
			if(null!=condition&&!Util.isNull(condition.getBiddingUnit())){
				condMap.put("biddingUnit", condition.getBiddingUnit());
			}
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			if(null!=condition&&!Util.isNull(condition.getBiddingTime())){
				condMap.put("biddingTime", DateUtil.dateToString(condition.getBiddingTime()));
			}
			if(null!=condition&&!Util.isNull(condition.getApplyMan())){
				condMap.put("applyMan", condition.getApplyMan());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findBidBookByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<BidBookWrapper> findAllBidBook(BidBookConditionForm condition,Page page)throws BusinessException{
		List<BidBookWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getBidBookDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProjectName())){
				condMap.put("projectName", condition.getProjectName());
			}
			if(null!=condition&&!Util.isNull(condition.getBiddingUnit())){
				condMap.put("biddingUnit", condition.getBiddingUnit());
			}
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			if(null!=condition&&!Util.isNull(condition.getBiddingTime())){
				condMap.put("biddingTime", DateUtil.dateToString(condition.getBiddingTime()));
			}
			if(null!=condition&&!Util.isNull(condition.getApplyMan())){
				condMap.put("applyMan", condition.getApplyMan());
			}
			
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findBidBookByIdAndName",condMap,new HashMap(),page);
			BidBookWrapper wrapper;
			for(int i=0;i<list.size();i++){
				BidBook bo = (BidBook)list.get(i);
				User user=bo.getUser(domainProviderSetImpl);
				if(null!=user){
					bo.setSalesManName(user.getCname());
				}
				wrapper = new BidBookWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取标书列表失败！", e);
		}
		return wrapperList;
	}

	public void delBidBookById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getBidBookDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除标书失败！", e);
		}
	}
	
	public BidBookForm getById(String id)throws BusinessException{
		BidBookForm form = new BidBookForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			BidBook bo = (BidBook)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getBidBookDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取标书失败!", e);
		}
		return form;
	}
	public List<BidBookWrapper> findAllBidBookByStatus(BidBookConditionForm condition)throws BusinessException{
		List<BidBookWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getBidBookDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getStatus())){
				condMap.put("status", condition.getStatus());
			}
			if(null!=condition&&!Util.isNull(condition.getBidStatus())){
				condMap.put("bidStatus", condition.getBidStatus());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findBidBookByStatus",condMap,new HashMap());
			BidBookWrapper wrapper;
			for(int i=0;i<list.size();i++){
				BidBook bo = (BidBook)list.get(i);
				wrapper = new BidBookWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取标书列表失败！", e);
		}
		return wrapperList;
	}
	public void bidBookBidStatus(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			BidBook bo = (BidBook)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getBidBookDP(), id);
			bo.setBidStatus(1);
			bo.store(domainProviderSetImpl);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("对标书进行中标操作失败!", e);
		}
	}
	
	/**
	 * 审批标书
	 * @param id
	 * @param status
	 * @throws BusinessException
	 */
	public void checkBidBook(String id,String status)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			BidBook bo = (BidBook)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getBidBookDP(), id);
			if(!Util.isNull(status)){
				if(status.equals("2") && "swjl".equals(bo.getEntryRole())){
					status = "3";
				}
				bo.setStatus(Integer.parseInt(status));
			}
			bo.store(domainProviderSetImpl);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("对标书进行中标操作失败!", e);
		}
		
	}
}
